
(function(){
	if(!window.cell) window.cell={};
	if(!window.cell.element) window.cell.element={};
	if(!window.$e) window.$e = cell.element = function(key,parent) {
		if ( window == this || !this.init ){
			return new cell.element(key,parent);
		}
		return this.init(key,parent);
	}
	cell.element.fn = cell.element.prototype = {
		version: "2.0.0",
		parent:[],
		init: function(key,node) {
			if(!node){var node=document;}
			if(key.nodeType==1){
				this[0]=key;
				this.length=1;
				return this;
			}
			
			if (key.match(/^#/)){
				this[0]=node.getElementById(key.replace(/^#/,''));
				this.length=1;
				return this;
			}
			
			for (var i=0;i < node.getElementsByTagName(key).length;i++){
				this[i]=node.getElementsByTagName(key)[i];
			}
			this.length=node.getElementsByTagName(key).length;
			return this;

		},
		attr: function(key,val) {
			var ret=[];
			for (var i=0;i < this.length;i++){
				if(!val){
					ret.push(this[i].getAttribute(key));
				}else{
					this[i].setAttribute(key,val);
				}
			}
			
			if(!val){
				if(ret.length==1){
					return ret[0];
				}else{
					return ret;
				}
			}
		},
		append:function(tag,attr,val){
			if(tag.nodeType==1){
				this.parent[0]=this[0];
				this[0]=this[0].appendChild(tag);
				return this;
			}
			for (var i=0;i < this.length;i++){
				this.parent[i]=this[i];
				this[i]=this[i].appendChild(document.createElement(tag));
				if(typeof(attr)=="object"){
					for (var key in attr){
						this[i].setAttribute(key,attr[key]);
					}
				}
			}
			return this;
		},
		clear:function(){
			for (var i=0;i < this.length;i++){
				while (this[i].firstChild) {
					this[i].removeChild(this[i].firstChild);
				}
			}
			return this;
		},
		remove:function(){
			for (var i=0;i < this.length;i++){
				var parent=this[i].parentNode;
				parent.removeChild(this[i]);
			}
		},
		style:function(key,val){
			if(!val){
				//return getComputedStyle(this[0],'').getPropertyValue(key);
				return this[0].style[key]
			}else{
				this[0].style[key] = val;
				this.log(key+":"+this[0].style[key]);
			}
		},
		log: function(str) {
			Components.classes['@mozilla.org/consoleservice;1'].getService(Components.interfaces.nsIConsoleService).logStringMessage(str);
		},
		dom:function(i){
			if(!i) i=0;
			return this[i];
		},
		val:function(val){
			var ret=[];
			for (var i=0;i < this.length;i++){
				if(!val){
					ret.push(this[i].attr("value"));
				}else{
					this[i].attr("value",val);
				}
			}
			
			if(!val){
				if(ret.length==1){
					return ret[0];
				}else{
					return ret;
				}
			}
		},
		text:function(val){
			var ret=[];
			for (var i=0;i < this.length;i++){
				if(!val){
					ret.push(this[i].contentText("value"));
				}else{
					this[i].contentText("value",val);
				}
			}
			
			if(!val){
				if(ret.length==1){
					return ret[0];
				}else{
					return ret;
				}
			}
		},
	}

})();
